<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<?php
if(!isset($_SESSION)){
	session_start();
}

if(!isset($_SESSION["adminName"]) || (empty($_SESSION["adminName"]))){
	header ("Location: ../index.php");
}
?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit Page</title>
    <link href="../../Styles/adminstype.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../js/admin_script.js"></script>
</head>
<body>
<?php
	$TenLSP = "";
	$getHinhAnh = "";
	$getGia = "";
	$getNgay = "";
	$getSoLuong = "";
	$getMoTa = "";
	$getMoTaCT = "";
	$getLoaiSP = "";
	$getNhaSX = "";
	$BiXoa = "";
	$checked = "";
	$id = $_SESSION["edit"];
	
	if(empty($_REQUEST["btnSua"])){
		if($id != "") // kiem tra du lieu
		{
			$sql = "select * from sanpham where MaSanPham = $id";
				
			require_once "../database/DataProvider.php";
			$result =  DataProvider::ExecuteQuery($sql);
				
			if(!empty($result))
			{				
				while ($row=mysql_fetch_array($result)){
					$TenLSP = $row["TenSanPham"];
					$getHinhAnh = $row["HinhURL"];
					$getGia = $row["GiaSanPham"];
					$getNgay = $row["NgayNhap"];
					$getSoLuong = $row["SoLuongTon"];
					$getMoTa = $row["MoTa"];
					$getMoTaCT = $row["MoTaChiTiet"];
					$getLoaiSP = $row["MaLoaiSanPham"];
					$getNhaSX = $row["MaHangSanXuat"];
					$BiXoa = $row["BiXoa"];
					$checked = ($row["BiXoa"] == 0 ? '' : 'checked');
				}	
			}
		}
	}

	$comboBox_LoaiSP = "";
	$comboBox_NhaXS = "";

	if(empty($_REQUEST["btnSua"]))//ko yeu cau them
	{
		// tạo combo Loai SP
		$width1 = 160;;
		$name1 ="LoaiSP";
		$active_1 = $getLoaiSP;
		$comboBox_LoaiSP = '<select style="width:'.$width1.'px" name="'.$name1.'" id="'.$name1.'" >'."\n";
		
		$sql = "select * from loaisanpham where BiXoa <> 1";
		require_once "../database/DataProvider.php";
		$result =  DataProvider::ExecuteQuery($sql);
		
		if(!empty($result)){
			while($row = mysql_fetch_array($result))
			{
				$comboBox_LoaiSP .= '<option value="'.$row["MaLoaiSanPham"].'"';
				if($active_1 == $row["MaLoaiSanPham"]) $comboBox_LoaiSP .= ' selected="selected" ';
				$comboBox_LoaiSP .= '>'.$row["TenLoaiSanPham"]."</option>\n";
			}
		}
		$comboBox_NhaXS .= "</select>\n";
		//tao conbo nha SX
		$width2 = 160;;
		$name2 ="NhaSX";
		$active_2 = $getNhaSX;
		$comboBox_NhaXS = '<select style="width:'.$width2.'px" name="'.$name2.'" id="'.$name2.'" >'."\n";
		
		$sql = "select * from hangsanxuat where BiXoa <> 1";
		require_once "../database/DataProvider.php";
		$result =  DataProvider::ExecuteQuery($sql);
		
		if(!empty($result)){
			while($row = mysql_fetch_array($result))
			{
				$comboBox_NhaXS .= '<option value="'.$row["MaHangSanXuat"].'"';
				if($active_2 == $row["MaHangSanXuat"]) $comboBox_NhaXS .= ' selected="selected" ';
				$comboBox_NhaXS .= '>'.$row["TenHangSanXuat"]."</option>\n";
			}
		}
		$comboBox_NhaXS .= "</select>\n";
	}
?>
<table>
	<tr class="boxContentTitle">
		<td>Sửa thông tin sản phẩm</td>
	</tr>
	<tr>
		<td>
			 <form action="resource/form_edit_product.php" method="post" name="form_edit_product" enctype="multipart/form-data">
				<table class="tableBoxView">
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Tên sản phẩm</td>
						<td><input type="text" maxlength="500" name="txtTen" id="txtTen" <?php echo("value='$TenLSP'");?>/></td>
					</tr>
					<tr>
						<td style="width: 200px"><input type="hidden" maxlength="500" name="txtHinh" id="txtHinh" <?php echo("value='$getHinhAnh'");?>/><td>
						<td style="width: 200px">Hình ảnh</td>
						<td><?php echo('<img src="~/../../images/'. $getHinhAnh . '" width="111" alt="" height="109" />');?>
						<br><input type="file" name="image" id="image" /></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Giá sản phẩm</td>
						<td><input type="text" name="txtGia" id="txtGia" <?php echo("value='$getGia'");?>/></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Số lượng</td>
						<td><input type="text" maxlength="9" name="txtSoluong" id="txtSoluong" <?php echo("value='$getSoLuong'");?>/></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Mô tả</td>
						<td><textarea style="height: 100px; width: 500px" name="txtMota" id="txtMota"><?php echo($getMoTa);?></textarea></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Mô tả chi tiết</td>
						<td><textarea style="height: 100px; width: 500px" name="txtMotaCT" id="txtMotaCT"><?php echo($getMoTaCT);?></textarea></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Loại sản phẩm</td>
						<td><?php echo($comboBox_LoaiSP); ?></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Nhà sản xuất</td>
						<td><?php echo($comboBox_NhaXS); ?></td>
					</tr>
					<tr>
						<td><td>
						<td>Bị xóa</td>
						<td><input type="checkbox" name="Xoa" value="Yes"<?php echo(" ");  echo($checked);?>></td>
					</tr>
					<tr>
						<td><td>
						<td></td>
						<td><input type="submit" name="btnSua" value="Save" onclick="return kiemTraTxt3('txtTen','txtGia','txtSoluong')"/></td>
					</tr>
				</table>
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<?php
			if(!empty($_REQUEST["btnSua"]))//có yeu cau sua
			{
				$tenSP = $_REQUEST["txtTen"];
				$hinhAnh = $_REQUEST["txtHinh"];
				$gia = $_REQUEST["txtGia"];
				$soLuong = $_REQUEST["txtSoluong"];
				$moTa = $_REQUEST["txtMota"];
				$moTaCT = $_REQUEST["txtMotaCT"];
				$loaiSP = $_REQUEST["LoaiSP"];
				$nhaSX = $_REQUEST["NhaSX"];
				$xoa = 0;
				if(isset($_REQUEST["Xoa"]) && $_REQUEST["Xoa"] == "Yes"){
					$xoa = 1;
				}

				
				if($tenSP != "") //  kiểm tra 
				{
					if ($_FILES['image']['name'] != NULL)
					{
						$dir="../../images/";
						$file= $dir. basename($_FILES['image']['name']);
						if (move_uploaded_file($_FILES['image']['tmp_name'],$file)){
							$hinhAnh = $_FILES['image']['name'];
						}
					}
					
					
					
					$sql = "UPDATE sanpham SET TenSanPham='" .$tenSP."',
										HinhURL='" . $hinhAnh. "',
										GiaSanPham=" . $gia .",
										SoLuongTon=" . $soLuong . ",
										MoTa='" . $moTa . "',
										MoTaChiTiet='" . $moTaCT . "',
										MaLoaiSanPham=" . $loaiSP . ",
										MaHangSanXuat=" . $nhaSX . ",
					 					BiXoa=$xoa WHERE MaSanPham=$id";
					require_once "../../database/DataProvider.php";
					$result =  DataProvider::ExecuteQuery($sql);
				}
				header ("Location: ../admin_manager_product.php");
			}
			?>
		</td>
	</tr>
</table>
</body>
</html>
